package com.doone.telmarket.action.web;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.JSONUtil;
import org.springframework.beans.factory.annotation.Autowired;

import com.doone.telmarket.action.admin.BaseActionSupport;
import com.doone.telmarket.action.admin.ServiceAction;
import com.doone.telmarket.domain.ServicePage;
import com.doone.telmarket.domain.UserInfo;
import com.doone.telmarket.domain.ibator.ServiceKeep;
import com.doone.telmarket.service.BusinessHandingService;
import com.doone.telmarket.service.ServiceKeepOperation;
import com.doone.telmarket.service.ServiceOperation;
import com.doone.telmarket.service.ServiceProductOperation;
import com.doone.telmarket.vo.ServiceOrderRelationResult;
import com.doone.telmarket.vo.ServiceOrderResult;

import freemarker.template.SimpleDate;

/**
 * @package
 * @author 甘雨波
 * @description  增值业务订购及相关操作用ACTION
 * @date 2009-10-21
 */
public class ValueAddedAction extends BaseActionSupport {

	/**
	 * 
	 */
	Logger log = Logger.getLogger(ServiceAction.class);
	
	private static final long serialVersionUID = 1L;
	
	private String cityCode;
	
	private String accnbr;
	
	private String serviceCode;
	
	private String pageUrl;
	
	private String serviceName;
	
	private String productid;
	
	private String businame;
	
	private String fee;
	
	private String orderMode;
	
	private String accessNumber;
	
	private String resultCode;
	
	private String resulMsg;
	
	private String keepId;
	
	@Autowired
	private ServiceKeepOperation remoteServiceKeepOperation;
	
	@Autowired
	private BusinessHandingService businessHandingService;
	
	@Autowired
	private ServiceOperation remoteServiceOperation;
	
	@Autowired
	private ServiceProductOperation remoteServiceProductOperation;
	
	@Action(value = "buyValueAdded", results = {
			@Result(name = "success", location = "/web/valueAddedResult.jsp")})
	public String buyValueAdded(){
		log.info("order");
		try{
			UserInfo user = (UserInfo)session.get("userInfo");
			
			log.info(user.getAccnbr()+"   "+user.getCityCode()+"   "+
					accessNumber+"   "+productid+"   "+orderMode+"   ");
			
			ServiceOrderResult serviceResult = businessHandingService.order(user.getAccnbr(), user.getCityCode(),
					accessNumber, productid, orderMode);
			resultCode = serviceResult.getResultCode();
			 
			resulMsg =serviceResult.getMessage();
			if("0".equals(resultCode)){
				resulMsg +=",生效时间:"+new SimpleDateFormat("yyyy年MM月dd日hh时mm分ss秒").format(new SimpleDateFormat("yyyyMMddhhmmss").parse(serviceResult.getEffectiveTime()));
			}
			//log.info(resultCode+"   "+resulMsg);
			return "success";
		}catch (Exception e) {
		    e.printStackTrace();
		    resultCode = "-1";
			resulMsg  = "尊敬的用户,很抱歉,系统繁忙,请您稍后使用此项功能";
			//log.info(resultCode+"   "+resulMsg);
			return "success";
		}
	}
	
	@Action(value = "cancelValueAdded", results = {
			@Result(name = "success", location = "/web/valueAddedResult.jsp")})
	public String cancelValueAdded(){
		log.info("quit order");
		try{
			UserInfo user = (UserInfo)session.get("userInfo");
			
			log.info(user.getAccnbr()+"   "+user.getCityCode()+"   "+
					accessNumber+"   "+productid+"   "+orderMode+"   ");
			
			ServiceOrderResult serviceResult = businessHandingService.quitOrder(user.getAccnbr(), user.getCityCode(),
					accessNumber, productid, orderMode);
			
			resultCode = serviceResult.getResultCode();
			 
			resulMsg =serviceResult.getMessage();
			
			if("0".equals(resultCode)){
				resulMsg +=",失效时间:"+new SimpleDateFormat("yyyy年MM月dd日hh时mm分ss秒").format(new SimpleDateFormat("yyyyMMddhhmmss").parse(serviceResult.getExpireTime()));
			}
			//log.info(resultCode+"   "+resulMsg);
			return "success";
		}catch (Exception e) {
		    e.printStackTrace();
		    resultCode = "-1";
			resulMsg  = "尊敬的用户,很抱歉,系统繁忙,请您稍后使用此项功能";
			log.info(resultCode+"   "+resulMsg);
			return "success";
		}
	}
	
	@SuppressWarnings("static-access")
	@Action("cancelValueAddedAjax")
	public void cancelValueAddedAjax(){
		try{
			UserInfo user = (UserInfo)session.get("userInfo");
			
			log.info(user.getAccnbr()+"   "+user.getCityCode()+"   "+
					accessNumber+"   "+productid+"   "+orderMode+"   ");
			
			ServiceOrderResult serviceResult = businessHandingService.quitOrder(user.getAccnbr(), user.getCityCode(),
					accessNumber, productid, orderMode);
			JSONUtil json = new JSONUtil();
			String jsonString = json.serialize(serviceResult);
		log.info("srr:"+serviceResult);
		log.info("srr:"+jsonString);
			responseWrite(jsonString);
			log.info(resultCode+"   "+resulMsg);
		}catch (Exception e) {
		    e.printStackTrace();
		    ServiceOrderResult serviceResult = new ServiceOrderResult("-1", "系统繁忙,请您稍后再试");
		    JSONUtil json = new JSONUtil();
		    String str;
			try {
				str = json.serialize(serviceResult);
				responseWrite(str);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		    
		}
	}
	
	/**
	 * @package
	 * @author 甘雨波
	 * @description n增加用户收藏信息
	 * @date 2009-10-22
	 */
	@Action("keepValueAdded")
	public void keepValueAdded(){
		try{
			if(session.get("userInfo")==null){
				responseWrite("尊敬的客户,很抱歉,请您登陆后使用收藏功能");
			}else{
				UserInfo user = (UserInfo)session.get("userInfo");
				ServiceKeep serviceKeep = new ServiceKeep();
				serviceKeep.setCitycode(user.getCityCode());
				serviceKeep.setUserId(user.getAccnbr());
				serviceKeep.setKeepTime(new Date());
				
				ServicePage servicePage = remoteServiceOperation.selectServicePageByServiceId(serviceCode);
				serviceKeep.setServicePage(servicePage);
				serviceKeep.setServiceName(serviceName);
				serviceKeep.setPageUrl(pageUrl);
				
				//log.info(pageUrl);
				remoteServiceKeepOperation.saveServiceKeep(serviceKeep);
				responseWrite("尊敬的客户,您好,已成功为您收藏此项增值业务");
			}
		}catch (Exception e) {
			e.printStackTrace();
			try {
				responseWrite("尊敬的客户,很抱歉,收藏增值业务失败");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		
		
	}
	@Action(value = "queryKeep", results = {
			@Result(name = "success", location = "/web/common/sc.jsp"),
			@Result(name = "error", location = "/web/error.jsp")})
	public String queryKeep(){
		try {
			UserInfo user = (UserInfo)session.get("userInfo");
			
			if(user==null){
				return "success";
			}
			
			String accnbr = user.getAccnbr();
			String cityCode = user.getCityCode();
			ServiceKeep sk = new ServiceKeep();
			sk.setCitycode(cityCode);
			sk.setUserId(accnbr);
			List<ServiceKeep> keepList =  remoteServiceKeepOperation.selectByServiceKeep(sk);
			JSONUtil json = new JSONUtil();
			session.put("mysc", keepList);
			log.info(json.serialize(keepList));
			return "success";
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
		
	}
	@Action("deleteKeep")
	public void deleteKeep(){
		try{
			remoteServiceKeepOperation.deleteByPrimaryKey(Long.parseLong(keepId));
			responseWrite("删除已收藏增值业务成功");
		}catch (Exception e) {
			e.printStackTrace();
			try {
				responseWrite("删除已收藏增值业务失败");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}
	
	@SuppressWarnings("static-access")
	@Action("queryValueAdded")
	public void queryValueAdded(){
		try{
			UserInfo user = (UserInfo)session.get("userInfo");
			String cityCode = user.getCityCode();
			ServiceOrderRelationResult srr =  businessHandingService.queryOrderRelation(cityCode, accessNumber, orderMode);
			JSONUtil json = new JSONUtil();
			String jsonString = json.serialize(srr);
			log.info("srr:"+srr);
			
			log.info("srr:"+jsonString);
			responseWrite(jsonString);
		}catch (Exception e) {
			e.printStackTrace();
			try {
				responseWrite("获取定购信息失败");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		
		
	}
	
	@Action("getBuyCountAndScCount")
	public void getBuyCountAndScCount(){
		try{
			int buyCount = remoteServiceProductOperation.selectBuyCount(productid);
			int scCount = remoteServiceProductOperation.selectScCount(Integer.parseInt(serviceCode));
			responseWrite(buyCount+"~"+scCount);
		}catch (Exception e) {
			e.printStackTrace();
			try {
				responseWrite("F");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}
	

	public String getCityCode() {
		return cityCode;
	}

	public void setCityCode(String cityCode) {
		this.cityCode = cityCode;
	}

	public String getAccnbr() {
		return accnbr;
	}

	public void setAccnbr(String accnbr) {
		this.accnbr = accnbr;
	}

	public String getOrderMode() {
		return orderMode;
	}

	public void setOrderMode(String orderMode) {
		this.orderMode = orderMode;
	}

	public String getServiceCode() {
		return serviceCode;
	}

	public void setServiceCode(String serviceCode) {
		this.serviceCode = serviceCode;
	}

	

	public String getPageUrl() {
		return pageUrl;
	}

	public void setPageUrl(String pageUrl) {
		this.pageUrl = pageUrl;
	}

	public String getServiceName() {
		return serviceName;
	}

	public void setServiceName(String serviceName) {
		this.serviceName = serviceName;
	}

	public String getProductid() {
		return productid;
	}

	public void setProductid(String productid) {
		this.productid = productid;
	}

	public String getBusiname() {
		return businame;
	}

	public void setBusiname(String businame) {
		this.businame = businame;
	}

	public String getFee() {
		return fee;
	}

	public void setFee(String fee) {
		this.fee = fee;
	}

	public String getAccessNumber() {
		return accessNumber;
	}

	public void setAccessNumber(String accessNumber) {
		this.accessNumber = accessNumber;
	}

	public String getResultCode() {
		return resultCode;
	}

	public void setResultCode(String resultCode) {
		this.resultCode = resultCode;
	}

	public String getResulMsg() {
		return resulMsg;
	}

	public void setResulMsg(String resulMsg) {
		this.resulMsg = resulMsg;
	}

	public String getKeepId() {
		return keepId;
	}

	public void setKeepId(String keepId) {
		this.keepId = keepId;
	}
	
	

}
